package cn.dnuihyy406.dnuifood_16110100406.controller.mefragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import cn.dnuihyy406.dnuifood_16110100406.R;
import cn.dnuihyy406.dnuifood_16110100406.bean.GetUserBean;
import cn.dnuihyy406.dnuifood_16110100406.listener.OnRetrofitListener;
import cn.dnuihyy406.dnuifood_16110100406.model.UserImformationModel;

public class MeFragment extends Fragment implements OnRetrofitListener<Object>{
    private ImageView mypic,mysetting;
    private TextView myname,mytitle,myphonenum,myorders,mycomment;
    SharedPreferences sharedPreferences;
    String user_id;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.me_fragment,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initViews();
        initData();
        initEvents();
    }


    @Override
    public void onSuccess(Object object, int flag) {
        switch (flag){
            case 12:
                myname.setText(((GetUserBean)object).getUsername());
                myphonenum.setText(((GetUserBean)object).getMobilenum());
                break;

            default:
                break;
        }
    }

    @Override
    public void onFailure(String msg) {

    }

    void initViews(){
        mypic=getActivity().findViewById(R.id.mypic);
        mysetting=getActivity().findViewById(R.id.mysetting);
        myname=getActivity().findViewById(R.id.myname);
        myphonenum=getActivity().findViewById(R.id.myphonenum);
        mycomment=getActivity().findViewById(R.id.mycomment);
        myorders=getActivity().findViewById(R.id.myorder);
    }

    void initData(){
        sharedPreferences = getActivity().getSharedPreferences("hyy", Context.MODE_PRIVATE);
        user_id = sharedPreferences.getString("userid","");
    }

    void initEvents(){
        showuserimformation();
        updateuserimformation();
        gotomyorder();
        getmycomment();
    }

    void showuserimformation(){
        UserImformationModel userImformationModel=new UserImformationModel();
        userImformationModel.getUserById(user_id,this);
    }

    void updateuserimformation(){
        mysetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(),UpdateUserActivity.class);
                startActivity(intent);
            }
        });
    }

    void gotomyorder(){
        myorders.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(),MyOrderActivity.class);
                startActivity(intent);
            }
        });
    }

    void getmycomment(){
        mycomment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(),MyCommentActivity.class);
                startActivity(intent);
            }
        });
    }
}
